﻿using DAL;
using Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project_V1
{
    public partial class FrmAlarm : Form
    {
        ReportService objReportService = new ReportService();
        private DateTime t1;
        private DateTime t2;
        public FrmAlarm()
        {
            InitializeComponent();
            this.Load += FrmAlarm_Load;
        }
       // int count = 0;
        private void FrmAlarm_Load(object sender, EventArgs e)
        {
            this.dgv_data.AutoGenerateColumns = false;
            //初始化combox
            this.cmb_TrendType.Items.Add("实时报警");
            this.cmb_TrendType.Items.Add("历史报警");
            this.cmb_TrendType.SelectedIndex = 0;

            this.cmb_Category.SelectedIndex = 0;
            //初始化count
            txt_count_TextChanged(null, null);
            //4、事件绑定方法
            //CommonMethods.update += CommonMethods_update;
            UpdateDGV();
            btn_Query.PerformClick();

            this.dateTimePicker2.Text = this.dateTimePicker2.Value.AddDays(-300).ToString();
        }

        //多线程
        private void CommonMethods_update()
        {
            this.dgv_data.Invoke(new Action(() =>
            {
                UpdateDGV();
            }
            ));
              
        }

        private void UpdateDGV()
        {

        }

        private void ModifyBackColor()
        {

        }

        private void txt_count_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb_PadNum_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmKeyPadNum.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmKeyPadNum frmSoftKey = new FrmKeyPadNum(tb_temp.Text);
            // 计算窗体的位置
            //textboxLocation.X = 1;
            //textboxLocation.Y = 1;
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmKeyPadNum.confirm)
            {
                tb_temp.Text = FrmKeyPadNum.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
            }
        }

        private void dgv_data_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {

            //for (int i = 0; i < this.dgv_data.Rows.Count; i++)
            //{
            //    if (dgv_data.Rows[i].Cells["AlarmType"].Value.ToString() == "开始报警")
            //    {
            //        dgv_data.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
            //    } 
            //}

            try
            {
                for (int i = 0; i < this.dgv_data.Rows.Count; i++)
                {
                    if (dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("故障") != -1)
                    {
                        dgv_data.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                    }

                    if (dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("不成功") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("硬限位") != -1)
                    {
                        dgv_data.Rows[i].DefaultCellStyle.ForeColor = Color.Yellow;
                    }


                    if (dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("已上电") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("已准备") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("已使能") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("模式中") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("测试中") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("已按下") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("已达到") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("进行中") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("停止中") != -1 ||
                        (dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("成功") != -1 && dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("不成功") == -1) ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("判断") != -1 ||
                        dgv_data.Rows[i].Cells[2].Value.ToString().IndexOf("调试中") != -1

                        
                        )
                    {
                        dgv_data.Rows[i].DefaultCellStyle.ForeColor = Color.Green;
                    }



                }
            }
            catch
            {


            }

            //string keyword = "特定关键字";

            //// 获取当前行的单元格值
            //string cellValue = e.Row.Cells[你想要匹配关键字的列索引].Value.ToString();


            //// 如果单元格值包含关键字，则更改该行的背景色
            //if (cellValue.Contains(keyword))
            //{
            //    e.Row.DefaultCellStyle.BackColor = Color.LightSalmon; // 可以更改为你想要的颜色
            //}




        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            string sql = "Select top("+ txt_count.Text + ")  CONVERT(varchar(100),InsertTime,20) as InsertTime,CONVERT(varchar(100),EndTime,20) as EndTime,VarName,AlarmState,Priority,AlarmType,iif( Value = 'True',N'开始报警',N'结束报警') as Value,AlarmValue,Operator,Note from AlarmData where 1=1";

            if (this.cmb_TrendType.Text == "实时报警")
            {
                //sql += " and Value = 'True' ";
            }
            else
            {
                DateTime t1 = Convert.ToDateTime(this.dt_Start.Text + " 00:00:00");
                DateTime t2 = Convert.ToDateTime(this.dt_End.Text + " 23:59:59");
                if (t1 > t2)
                {
                    MessageBox.Show("开始时间与结束时间不符合，请检查", "查询提示");
                    return;
                }
                TimeSpan ts = t2 - t1;
                if (ts.TotalDays > 7.0)
                {
                    MessageBox.Show("查询范围太大", "查询提示");
                    return;
                }
                sql += " and InsertTime between '{0}' and '{1}'";
                sql = string.Format(sql, t1, t2);
            }

            if (this.cmb_Category.Text == "全部")
            {
                sql += "   ";
            }
            else if (this.cmb_Category.Text == "故障类")
            {
                sql += " and Category = '故障类' ";
            }
            else if (this.cmb_Category.Text == "提示类")
            {
                sql += " and Category = '提示类' ";
            }
            else if (this.cmb_Category.Text == "警告类")
            {
                sql += " and Category = '警告类' ";
            }

            sql += " Order by Id DESC";

            this.dgv_data.DataSource = SQLHelper.GetDataSet(sql).Tables[0];
            ModifyBackColor();
        }

        private void cmb_TrendType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FrmAlarm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.cmb_TrendType.Text == "实时报警")
            {
                btn_Query.PerformClick();
            }
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否删除历史记录", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
            }
            else
            {
                try
                {
                    t1 = Convert.ToDateTime(this.dateTimePicker2.Text); 
                    objReportService.DeleteAlerm(t1);//查询后存入报表集合
                }
                catch
                {
                }
            }
        }
    }
}
